package org.example.demo.broker;

import com.google.common.base.Preconditions;
import org.example.demo.domain.Message;
import org.example.demo.domain.MessageProducer;
import org.example.demo.domain.MessageType;
import org.example.demo.domain.SendCallback;
import org.example.demo.domain.exception.MessageRunTimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class ProducerClient implements MessageProducer {
    @Autowired
    private RabbitBroker broker;

    @Override
    public void send(Message message, SendCallback sendCallback) throws MessageRunTimeException {

    }

    @Override
    public void send(Message message) throws MessageRunTimeException {
        Preconditions.checkNotNull(message.getExchange());
        Preconditions.checkNotNull(message.getMessageType());
        this.broker.send(message);
    }

    @Override
    public void send(List<Message> messages) throws MessageRunTimeException {
        messages.forEach(message -> {
            message.setMessageType(MessageType.RAPID);
            MessageHolder.add(message);
        });
        this.broker.sendMessages();
    }
}
